<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>添加采购单管理</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link href="../../layui/css/layui.css" rel="stylesheet">
</head>
<body>
<div class="layui-layout layui-layout-admin">
    <!---头部导航栏-->
    <div class="layui-header">
        <div class="layui-logo layui-hide-xs layui-bg-black"> <img src="//unpkg.com/outeres@0.0.10/img/layui/icon-v2.png" class="layui-nav-img">小药店管理系统</div>
        <!-- 头部区域（可配合layui 已有的水平导航） -->
        <ul class="layui-nav layui-layout-left">
            <!-- 移动端显示 -->
            <li class="layui-nav-item layui-show-xs-inline-block layui-hide-sm" lay-header-event="menuLeft">
                <i class="layui-icon layui-icon-spread-left"></i>
            </li>
            <li class="layui-nav-item layui-hide-xs"><a href="javascript:;">前台页面</a></li>
        </ul>
        <ul class="layui-nav layui-layout-right">
            <li class="layui-nav-item layui-hide layui-show-sm-inline-block">
                <a href="javascript:;">
                    <img src="/Users/chenminggang/Downloads/20200829200211_enkci.jpg" class="layui-nav-img" >
                    管理员
                </a>
                <dl class="layui-nav-child">
                    <dd><a href="javascript:;">文件</a></dd>
                    <dd><a href="javascript:;">设置</a></dd>
                    <dd><a href="javascript:;">登出</a></dd>
                </dl>
            </li>
            <li class="layui-nav-item" lay-header-event="menuRight" lay-unselect>
                <a href="javascript:;">
                    <i class="layui-icon layui-icon-more-vertical"></i>
                </a>
            </li>
        </ul>
    </div>

    <!---菜单导航栏-->
    <div class="layui-side layui-bg-black">
        <div class="layui-side-scroll">
            <!-- 左侧导航区域（可配合layui已有的垂直导航） -->
            <ul class="layui-nav layui-nav-tree" lay-filter="test">
                <li class="layui-nav-item layui-nav-itemed">
                    <a class="" href="javascript:;">采购管理</a>
                    <dl class="layui-nav-child">
                        <dd><a href="./gongyingshangxinxi.html">供应商信息管理</a></dd>
                        <dd><a href="./yaopinxinxi.html">药品信息管理</a></dd>
                        <dd class="layui-this"><a href="javascript;">采购单添加</a></dd>
                        <dd><a href="./PurchaseDetail.html">采购明细管理</a></dd>
                    </dl>
                </li>
                <li class="layui-nav-item">
                    <a href="javascript:;">销售管理</a>
                    <dl class="layui-nav-child">
                        <dd><a href="../salemanage/addsale.html">销售单添加管理</a></dd>
                        <dd><a href="../salemanage/saledetailmanage.html">销售明细管理</a></dd>
                    </dl>
                </li>
                <li class="layui-nav-item">
                    <a href="javascript:;">药店统计管理</a>
                    <dl class="layui-nav-child">
                        <dd><a href="../medicalstatisticmanage/Inboundstatistic.html">入库统计</a></dd>
                        <dd><a href="../medicalstatisticmanage/salestatistic.html">销售统计</a></dd>
                        <dd><a href="../medicalstatisticmanage/inventoryinquiry.html">库存查询</a></dd>
                    </dl>
                </li>

                <li class="layui-nav-item">
                    <a href="javascript:;">人事管理</a>
                    <dl class="layui-nav-child">
                        <dd><a href="../peoplemanage/addpeople.html">员工添加</a></dd>
                        <dd><a href="../peoplemanage/managepeople.html">员工管理</a></dd>
                    </dl>
                </li>

                <li class="layui-nav-item">
                    <a href="javascript:;">打印管理</a>
                    <dl class="layui-nav-child">
                        <dd><a href="../print/enterlibrary.html">入库单</a></dd>
                        <dd><a href="../print/exitlibrary.html">出库单</a></dd>
                    </dl>
                </li>

                <li class="layui-nav-item">
                    <a href="javascript:;">系统管理</a>
                    <dl class="layui-nav-child">
                        <dd><a href="../systemmanage/updatepassword.html">修改密码</a></dd>
                    </dl>
                </li>
            </ul>
        </div>
    </div>

    <!---内容-->
    <div class="layui-body">
        <!-- 内容主体区域 -->
        <div style="padding: 15px;">
            <blockquote class="layui-elem-quote layui-text">
                首页 / 采购明细 / 添加采购单
            </blockquote>
            <div class="layui-card layui-panel">
                <div class="layui-card-body">
                    <form id="purchaseForm">
                        <div class="layui-form" lay-filter="formTest">
                            <div class="layui-form-item">
                                <label class="layui-form-label">采购单号</label>
                                <div class="layui-input-block">
                                    <input type="text" name="danjuhao" required  lay-verify="required" placeholder="请输入采购单号" autocomplete="off" class="layui-input">
                                </div>
                            </div>
                            <div class="layui-form-item">
                                <label class="layui-form-label">采购时间</label>
                                <div class="layui-input-block">
                                    <input type="text" name="shijian" required lay-verify="required" placeholder="请输入采购时间" autocomplete="off" class="layui-input" id="purchaseDate" readonly>
                                </div>
                            </div>
                            <div class="layui-form-item">
                                <label class="layui-form-label">药品总金额</label>
                                <div class="layui-input-block">
                                    <input type="number" name="zongjiage" required lay-verify="required" placeholder="请输入药品总金额" autocomplete="off" class="layui-input">
                                </div>
                            </div>
                            <div class="layui-form-item">
                                <label class="layui-form-label">药品分类</label>
                                <div class="layui-input-block">
                                    <select name="category" required lay-verify="required">
                                        <option value="">请选择药品分类</option>
                                        <option value="分类1">分类1</option>
                                        <option value="分类2">分类2</option>
                                        <!-- 添加更多分类选项 -->
                                    </select>
                                </div>
                            </div>
                            <div class="layui-form-item">
                                <label class="layui-form-label">采购负责人</label>
                                <div class="layui-input-block">
                                    <select name="jingshouren" required lay-verify="required" id="responsiblePerson">
                                        <option value="">请选择负责人</option>
                                        <!-- 动态加载负责人数据 -->
                                    </select>
                                </div>
                            </div>
                            <div class="layui-form-item layui-form-text">
                                <label class="layui-form-label">备注</label>
                                <div class="layui-input-block">
                                    <textarea name="beizhu" placeholder="请输入备注" class="layui-textarea"></textarea>
                                </div>
                            </div>
                            <div class="layui-form-item">
                                <div class="layui-input-block">
                                    <button class="layui-btn" lay-submit lay-filter="formDemo">添加</button>
                                </div>
                            </div>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </div>

    <!---底部导航栏-->
    <div class="layui-footer">
        <!-- 底部固定区域 -->
        底部固定区域
    </div>
</div>

<script src="../../layui/layui.js"></script>
<script>
    //JS
    layui.use(['element', 'layer', 'util', 'form', 'laydate'], function(){
        var element = layui.element;
        var layer = layui.layer;
        var util = layui.util;
        var form = layui.form;
        var laydate = layui.laydate;
        var $ = layui.$;

        // 初始化采购时间
        laydate.render({
            elem: '#purchaseDate',
            value: new Date()
        });

        // // 初始化负责人下拉框
        // $('#responsiblePerson').on('click', function() {
        //     $.ajax({
        //         url: '/ruku/rukuCaiName',
        //         type: 'GET',
        //         success: function(res) {
        //             if (res.code === 0) {
        //                 var responsiblePersonOptions = '<option value="">请选择负责人</option>';
        //                 res.data.forEach(function(person) {
        //                     responsiblePersonOptions += '<option value="' + person.id + '">' + person.name + '</option>';
        //                 });
        //                 $('#responsiblePerson').html(responsiblePersonOptions);
        //                 form.render('select');
        //             } else {
        //                 layer.msg('获取负责人数据失败');
        //             }
        //         },
        //         error: function() {
        //             layer.msg('请求失败，请重试');
        //         }
        //     });
        // });
        // 修改为页面加载时自动获取负责人数据
        $(document).ready(function() {
            $.ajax({
                url: '/ruku/user/ruku/rukuCaiName',
                type: 'GET',
                success: function(res) {
                    if (res.code === 0) {
                        var responsiblePersonOptions = '<option value="">请选择负责人</option>';
                        res.data.forEach(function(person) {
                            console.log(person);
                            responsiblePersonOptions += '<option value="' + person.id + '">' + person.name + '</option>';
                        });
                        $('#responsiblePerson').html(responsiblePersonOptions);
                        form.render('select');
                    } else {
                        layer.msg('获取负责人数据失败');
                    }
                },
                error: function() {
                    layer.msg('请求失败，请重试');
                }
            });
        });


        // 表单验证
        form.verify({
            required: function(value){
                if(value === ''){
                    return '该字段不能为空';
                }
            }
        });

        // 表单提交
        form.on('submit(formDemo)', function(data){
            var formData = data.field;
            // 验证每个控件的值是否为空
            for (var key in formData) {
                if (formData[key] === '') {
                    layer.msg('请填写所有必填项');
                    return false;
                }
            }
            // 提交表单
            $.ajax({
                url: '/ruku/user/ruku/rukuAdd', // 确保URL正确
                type: 'POST',
                contentType: 'application/json', // 设置请求头为 application/json
                data: JSON.stringify(formData), // 将表单数据转换为 JSON 字符串
                success: function(res) {
                    if (res.code === 0) {
                        layer.msg('采购单添加成功');
                    } else {
                        layer.msg('采购单添加失败');
                    }
                },
                error: function() {
                    layer.msg('请求失败，请重试');
                }
            });
            return false; // 阻止表单默认提交行为
        });
    });
</script>
</body>
</html>
